package com.sanjin.controller;

import com.sanjin.annotations.SysLogOperation;
import com.sanjin.convert.ParkLotConvert;
import com.sanjin.entity.ParkLot;
import com.sanjin.exception.MyException;
import com.sanjin.form.ParkLotForm;
import com.sanjin.form.interfaces.Create;
import com.sanjin.form.interfaces.Update;
import com.sanjin.service.ParkLotService;
import com.sanjin.utils.ConvertUtils;
import com.sanjin.utils.ResponseVoUtils;
import com.sanjin.vo.PageVo;
import com.sanjin.vo.ParkLotVo;
import com.sanjin.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/5 13:55
 */
@Api(tags = "停车场接口")
@RestController
@RequestMapping("/parkLot")
public class ParkLotController {

    @Autowired
    private ParkLotService parkLotService;

    @Autowired
    private ParkLotConvert parkLotConvert;

    @Autowired
    private ConvertUtils convertUtils;

    @SysLogOperation(value = "【创建停车场】")
    @ApiOperation("创建停车场")
    @PostMapping("")
    public ResponseVo create(@RequestBody @Validated(Create.class) ParkLotForm parkLotForm) {
        ParkLot parkLot = parkLotConvert.parkLotForm2ParkLot(parkLotForm);
        parkLotService.createParkLot(parkLot);
        return ResponseVoUtils.success(parkLot);
    }

    @SysLogOperation(value = "【删除停车场】")
    @ApiOperation("删除停车场")
    @DeleteMapping("")
    public ResponseVo delete(@RequestBody  List<Integer> ids) {
        if (ids == null) throw new MyException("ids must not be null");
        parkLotService.deleteByids(ids);
        return ResponseVoUtils.success();
    }

    @SysLogOperation(value = "【修改停车场】")
    @ApiOperation("修改停车场")
    @PutMapping("")
    public ResponseVo update(@RequestBody @Validated(Update.class) ParkLotForm parkLotForm) {
        ParkLot parkLot = parkLotConvert.parkLotForm2ParkLot(parkLotForm);
        parkLotService.updateParkLot(parkLot);
        return ResponseVoUtils.success(parkLot);
    }

    @ApiOperation("Page显示停车场")
    @GetMapping("/list")
    public ResponseVo list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                         @RequestParam(value = "size", defaultValue = "10") Integer size) {

        // 0 标识第一页
        PageRequest pageRequest = PageRequest.of(page - 1,size);
        Page<ParkLot> parkLotPage = parkLotService.queryParkLotByPage(pageRequest);
        PageVo<ParkLot> pagevo = convertUtils.page2PageVo(parkLotPage);
        return ResponseVoUtils.success(pagevo);
    }


    @ApiOperation("获取停车场信息")
    @GetMapping("/getParkLots")
    public ResponseVo getParkLots() {
        List<ParkLot> parkLotList = parkLotService.queryParkLots();

        List<ParkLotVo> parkLotVoList = parkLotList.stream().map(pl -> {
            return parkLotConvert.parkLot2ParkLotVo(pl);
        }).collect(Collectors.toList());

        return ResponseVoUtils.success(parkLotVoList);
    }

}
